using System;
using System.Collections.Generic;
using System.Text;

namespace RaisingStudio.Data.Common
{
    public class DataAdapterBase
    {
        protected System.Data.IDbConnection connection;
        /// <summary>
        /// database connection.
        /// </summary>
        public System.Data.IDbConnection Connection
        {
            get
            {
                return this.connection;
            }
        }

        protected DatabaseType _databaseType;
        /// <summary>
        /// Database type.
        /// </summary>
        public DatabaseType DatabaseType
        {
            get
            {
                return this._databaseType;
            }
            set
            {
                this._databaseType = value;
            }
        }

        protected string _providerName;
        /// <summary>
        /// The name of provider.
        /// </summary>
        public string ProviderName
        {
            get
            {
                return this._providerName;
            }
            set
            {
                this._providerName = value;
            }
        }

        protected TransactionManager transactionManager;
        /// <summary>
        /// database transaction manager.
        /// </summary>
        public TransactionManager TransactionManager
        {
            get
            {
                return this.transactionManager;
            }
        }
        
        #region log
        protected ILogManager _logManager;

        public ILogManager LogManager
        {
            get
            {
                return this._logManager;
            }
        }

        public virtual void WriteLine(string value)
        {
            if (this._logManager != null)
            {
                this._logManager.WriteLine(value);
            }
        }

        public virtual void WriteLine(string format, object arg0)
        {
            if (this._logManager != null)
            {
                this._logManager.WriteLine(format, arg0);
            }
        }

        public virtual void WriteLine(string format, params object[] arg)
        {
            if (this._logManager != null)
            {
                this._logManager.WriteLine(format, arg);
            }
        }


        public virtual void WriteLog(System.Data.IDbCommand command)
        {
            bool writeLog = (this._logManager != null && this._logManager.Log != null);
            if (writeLog)
            {
                string commandText = command.CommandText;
                WriteLine(commandText);
                foreach (System.Data.IDbDataParameter parameter in command.Parameters)
                {
                    // sample, -- @p0: Input NVarChar (Size = 6; Prec = 0; Scale = 0) [London]
                    WriteLine("-- {0}: {1} {2} (Size = {3}; Prec = {4}, Scale = {5}) [{6}]", parameter.ParameterName, parameter.Direction, parameter.DbType, parameter.Size, parameter.Precision, parameter.Scale, parameter.Value);
                }
                //sample, -- Context: OTHER, oracle.10.1
                WriteLine("-- Context: {0}, {1}", this._databaseType, this._providerName);
                WriteLine(string.Empty);
            }
        }

        public virtual void WriteLog(System.Data.IDbDataAdapter dataAdapter)
        {
            bool writeLog = (this._logManager != null && this._logManager.Log != null);
            if (writeLog)
            {
                WriteLine("-> [DataAdapter]:");
                if (dataAdapter.SelectCommand != null)
                {
                    System.Data.IDbCommand command = dataAdapter.SelectCommand;
                    string commandText = command.CommandText;
                    WriteLine(commandText);
                    foreach (System.Data.IDbDataParameter parameter in command.Parameters)
                    {
                        // sample, -- @p0: Input NVarChar (Size = 6; Prec = 0; Scale = 0) [London]
                        WriteLine("-- {0}: {1} {2} (Size = {3}; Prec = {4}, Scale = {5}) [{6}]", parameter.ParameterName, parameter.Direction, parameter.DbType, parameter.Size, parameter.Precision, parameter.Scale, parameter.Value);
                    }
                }
                if (dataAdapter.InsertCommand != null)
                {
                    WriteLine(dataAdapter.InsertCommand.CommandText);
                }
                if (dataAdapter.DeleteCommand != null)
                {
                    WriteLine(dataAdapter.DeleteCommand.CommandText);
                }
                if (dataAdapter.UpdateCommand != null)
                {
                    WriteLine(dataAdapter.UpdateCommand.CommandText);
                }
                WriteLine("-- Context: {0}, {1}", this._databaseType, this._providerName);
                WriteLine(string.Empty);
            }
        }
        #endregion
    }
}
